<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增订单')"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">


            <div class="layui-form-item">
                <label class="layui-form-label">客户昵称：</label>
                <div class="layui-input-block">
                    <input type="text" name="clientName" placeholder="请输入客户昵称"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系方式：</label>
                <div class="layui-input-block">
                    <input type="text" name="clientContact" placeholder="请输入客户联系方式"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">三方订单：</label>
                <div class="layui-input-block">
                    <input type="text" name="thirdOrderNumber" placeholder="请输入三方订单号"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否自做</label>
                <div class="layui-input-block">
                    <div class="radio-box" radio-dict-code="is_self_make" name="isSelfMake">

                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">订单来源</label>
                <div class="layui-input-block">
                    <div class="radio-box" radio-dict-code="order_source" name="orderSource"></div>
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">订单状态</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <div class="radio-box" radio-dict-code="order_status" name="orderStatus"></div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">群聊名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="groupName" placeholder="请输入群聊名称"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">接单人员：</label>
                <div class="layui-input-block">
                    <input type="text" name="assignedTo" placeholder="请输入接单人员"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">详细描述：</label>
                <div class="layui-input-block">
                    <input type="text" name="orderDetails" placeholder="请输入详细描述"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">订单金额：</label>
                <div class="layui-input-block">
                    <input type="text" name="orderAmount" placeholder="请输入订单金额"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">下单图片：</label>
                <div class="layui-input-block">
                    <div id="image-preview" style="display: none;">
                        <img src="" alt="Order Image" id="image-preview-img" style="width: 100px; height: auto; cursor: pointer;" onclick="viewImage(this.src)" />
                    </div>

                    <button type="button" class="layui-btn layui-btn-normal" id="upload-image-btn" style="background-color: #36b368">
                        <i class="layui-icon">&#xe67c;</i> 上传图片
                    </button>

                    <input type="hidden" name="orderImage" id="orderImage" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">交付日期：</label>
                <div class="layui-input-block">
                    <div class="input-group date">
                        <input type="text" name="finishTime" class="layui-input"
                               id="finishTime" lay-verify="required">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="orderDispatch-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
</body>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    layui.use(['form', 'jquery', 'laydate', 'dictionary','upload'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let upload = layui.upload;

        let prefix = "/sys/orderDispatch/"

        laydate.render({
            elem: '#finishTime'
        });

        var uploadInst = upload.render({
            elem: '#upload-image-btn',  // Upload button
            url: '/upload/image',  // Server-side upload URL (replace with your actual URL)
            accept: 'images',  // Only allow image files
            done: function(res) {
                // Success callback: Display the uploaded image preview
                if (res.success) {
                    var imageUrl = res.data;  // Assuming the response contains the image URL in `res.data.url`
                    $('#orderImage').val(imageUrl);  // Set the image URL in the hidden input field
                    $('#image-preview').show();  // Show the image preview
                    $('#image-preview-img').attr('src', imageUrl);  // Set the preview image source
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1000});  // Show error message
                }
            },
            error: function() {
                layer.msg("上传失败", {icon: 2, time: 1000});
            }
        });

        form.on('submit(orderDispatch-save)', function (data) {
            for (var key in data.field) {
                var type = $(data.form).find("input[name='" + key + "']").attr("type");
                if (type == "checkbox") {
                    var value = "";
                    $(data.form).find("input[name='" + key + "']:checked").each(function () {
                        value += $(this).val() + ",";
                    })
                    if (value != "") {
                        value = value.substr(0, value.length - 1);
                        data.field[key] = value;
                    }

                }
            }
            $.ajax({
                url: prefix + 'save',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.layui.table.reload("orderDispatch-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    });
</script>
</html>
